package com.wudi.estate.controller.ownermanagement;

import com.alibaba.druid.mock.MockBlob;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.homemanage.Buildings;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.ownermanagement.Pet;
import com.wudi.estate.bean.ownermanagement.Vehicle;
import com.wudi.estate.mapper.homemanage.BuildingsMapper;
import com.wudi.estate.service.communitymanagement.CommunityService;
import com.wudi.estate.service.homemanage.BuildingsService;
import com.wudi.estate.service.ownermanagement.PersonnelService;
import com.wudi.estate.service.ownermanagement.VehicleService;
import com.wudi.estate.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RequestMapping("vehicle")
@Controller
public class VehicleController {
    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private PersonnelService personnelService;

    @RequestMapping("listUi")
    public String vehicle_listUi(String vcard,ModelMap map){

        if (vcard==null){
            try {
                List<Vehicle> vehicles = vehicleService.selectAll();
                List<Map<String, Object>> vs = insertList(vehicles);
                map.addAttribute("vs",vs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else{
            try {
                List<Vehicle> vehicles = vehicleService.selectByName(vcard);
                List<Map<String, Object>> vs = insertList(vehicles);
                map.addAttribute("vs",vs);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return "vehicle_list";
    }
    public List<Map<String,Object>> insertList(List<Vehicle> vehicles) throws Exception {
        List<Map<String,Object>> vs = new ArrayList<>();
        Personnel p = new Personnel();
        for (Vehicle v: vehicles) {
            Map<String,Object> m = new HashMap<>();
            p.setId(v.getPid());
            Personnel p1 = new Personnel();
            p1= personnelService.selectById(p);
            m.put("vehicles",v);
            m.put("personnels",p1);
            vs.add(m);
        }
        return vs;
    }
    @RequestMapping("addUi")
    public String vehicle_addUi(ModelMap map){
        List<Personnel> pname = new ArrayList<>();
        try {
            pname = personnelService.selectAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        map.addAttribute("pname",pname);
        return "vehicle_add";
    }
    @RequestMapping("add")
    public String vehicle_add(Vehicle vehicle,MultipartFile headerFile){
        vehicle.setCreateTime(new Date());
        System.out.println(vehicle);
        String fileName = "";
        System.out.println(headerFile);

        try {
            fileName = FileUtils.fileUpload(headerFile);


        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        vehicle.setImg(fileName);

        try {
            vehicleService.insert(vehicle);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "redirect:/vehicle/listUi";
    }
    @RequestMapping("editUi")
    public String vehicle_editUi(long id,ModelMap map){
        Vehicle v = new Vehicle();
        List<Personnel> pname = new ArrayList<>();
        try {

            v.setId(id);
            v=vehicleService.selectById(v);
            pname = personnelService.selectAll();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(v);
        map.addAttribute("pname",pname);
        map.addAttribute("v",v);
        return "vehicle_edit";
    }
    @RequestMapping("edit")
    public String vehicle_edit(Vehicle vehicle,MultipartFile headerFile){
        vehicle.setCreateTime(new Date());
        System.out.println(vehicle);
        String fileName = "";
        System.out.println(headerFile);
        if(!headerFile.isEmpty()) {
            try {
                fileName = FileUtils.fileUpload(headerFile);


            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            vehicle.setImg(fileName);
        }
        try {
            vehicleService.updateById(vehicle);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return "redirect:/vehicle/listUi";
    }
    @RequestMapping("/update")
    public String pet_update_state(long id){
        Vehicle v = new Vehicle();
        v.setId(id);
        try {
            vehicleService.prohibit(v);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "redirect:/vehicle/listUi";
    }

}
